package com.jwaoo.mobile.view.main;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Toast;
import com.dx.as.UI;
import com.dx.as.common.FragmentUtils;
import com.dx.as.permission.MPermission;
import com.dx.as.permission.annotation.OnMPermissionDenied;
import com.dx.as.permission.annotation.OnMPermissionGranted;
import com.dx.as.permission.annotation.OnMPermissionNeverAskAgain;
import com.jwaoo.chat.fragment.session.SessionFragment;
import com.jwaoo.mobile.R;
import com.jwaoo.mobile.common.listener.JWOnClickListener;
import com.jwaoo.mobile.view.contacts.ContactsFragment;
import com.jwaoo.mobile.view.main.repository.MainPresenter;


public class MainActivity extends UI{

    private final int BASIC_PERMISSION_REQUEST_CODE = 100;
    private MainPresenter mainPresenter;
    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        requestBasicPermission();
        putDefaultFragment();
        mainPresenter = new MainPresenter(this);
        findViewById(R.id.menu1).setOnClickListener(onClickListener);
        findViewById(R.id.menu2).setOnClickListener(onClickListener);
        findViewById(R.id.menu3).setOnClickListener(onClickListener);
        mainPresenter.getDataFromNetOrDB();
    }

    private void putDefaultFragment(){
        Fragment fragment = new SessionFragment();
        FragmentUtils.putDefaultFragment(this,R.id.container,fragment);
    }

    /**
     * 基本权限管理
     */
    private final String[] BASIC_PERMISSIONS = new String[]{
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
            android.Manifest.permission.READ_EXTERNAL_STORAGE,
            android.Manifest.permission.CAMERA,
            android.Manifest.permission.READ_PHONE_STATE,
            android.Manifest.permission.RECORD_AUDIO,
            android.Manifest.permission.ACCESS_COARSE_LOCATION,
            android.Manifest.permission.ACCESS_FINE_LOCATION
    };

    private void requestBasicPermission() {
        MPermission.printMPermissionResult(true, this, BASIC_PERMISSIONS);
        MPermission.with(MainActivity.this)
                .setRequestCode(BASIC_PERMISSION_REQUEST_CODE)
                .permissions(BASIC_PERMISSIONS)
                .request();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        MPermission.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    @OnMPermissionGranted(BASIC_PERMISSION_REQUEST_CODE)
    public void onBasicPermissionSuccess() {
        Toast.makeText(this, "授权成功", Toast.LENGTH_SHORT).show();
        MPermission.printMPermissionResult(false, this, BASIC_PERMISSIONS);
    }

    @OnMPermissionDenied(BASIC_PERMISSION_REQUEST_CODE)
    @OnMPermissionNeverAskAgain(BASIC_PERMISSION_REQUEST_CODE)
    public void onBasicPermissionFailed() {
        Toast.makeText(this, "未全部授权，部分功能可能无法正常运行！", Toast.LENGTH_SHORT).show();
        MPermission.printMPermissionResult(false, this, BASIC_PERMISSIONS);
    }

    private View.OnClickListener onClickListener = new JWOnClickListener() {
        @Override
        public void onClick(View v) {
            super.onClick(v);
            switch (v.getId()){
                case R.id.menu1:
                    FragmentUtils.goToFragment(MainActivity.this,SessionFragment.class,R.id.container);
                    break;
                case R.id.menu2:
                    FragmentUtils.goToFragment(MainActivity.this,ContactsFragment.class,R.id.container);
                    break;
                case R.id.menu3:
                    break;
            }
        }
    };
}


